/*
 ** writen by hanv
 ** n2.vietha@gmail.com
 */
package vn.vng.research.fsd.test;

import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;


import vn.vng.research.fsd.Configuration;
import vn.vng.research.fsd.da.ProfilesDA;
import vn.vng.research.fsd.da.SchoolRefinerDA;
import vn.vng.research.fsd.data.FacetedTopDocs;
import vn.vng.research.fsd.data.SearchResult;
import vn.vng.research.fsd.data.User;
import vn.vng.research.fsd.index.Indexer;
import vn.vng.research.fsd.search.FacetedSearcher;
import vn.vng.research.fsd.services.thrift.TSchool;
import vn.vng.research.fsd.services.thrift.TUser;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		Configuration.load();
		SchoolRefinerDA.fetch();
		Indexer.index();

		SearchResult result = FacetedSearcher.matchAndGroup("minh",50,1,-1);
		FacetedTopDocs topDocs = result.getTopDocs();

		// show all
		System.out.println("Showing result:");

		List<TSchool> schools = topDocs.getSchoolLs();
		for (int i = 0; i < Math.min(schools.size(), 10); i++) {
			String schoolName = SchoolRefinerDA.getSchoolName(schools.get(i).getId());
			int profilesCount = topDocs.getSchoolGroups().get(schools.get(i))
					.size();
			System.out.println("School name: " + schoolName + "("
					+ profilesCount + ")");
		}

//		LinkedList<Integer> profiles = topDocs.getSchoolGroups().get(0);
//		for (int i = 0; i < profiles.size(); i++) {
//			TUser user = topDocs.getProfiles().get(profiles.get(i));
//			System.out.println("uid: " + user.getUid() + "|name: "
//					+ user.getUsername() + "|email: " + user.getEmail()
//					+ "|displayname: " + user.getDisplayname() + "|education: "
//					+ SchoolRefinerDA.getSchoolName(user.getEducation()) + "");
//		}

		// show 1 random school
		Random random = new Random(System.currentTimeMillis());
		int schoolID = schools.get(random.nextInt(schools.size())).getId();
		System.out.println("Showing result of: [" + schoolID + "] "
				+ SchoolRefinerDA.getSchoolName(schoolID));
		List<TUser> profilesGroup = topDocs.getSchoolGroups().get(
				schoolID);
		for (int i = 0; i < profilesGroup.size(); i++) {
			TUser user = topDocs.getProfiles().get(profilesGroup.get(i).getUid());
			System.out.println("uid: " + user.getUid() + "|name: "
					+ user.getUsername() + "|email: " + user.getEmail()
					+ "|displayname: " + user.getDisplayname() + "|education: "
					+ SchoolRefinerDA.getSchoolName(user.getEducation()) + "");
		}

		/*
		 * try { File resultFile = new File("" + "-search-result.txt"); if
		 * (!resultFile.exists()) resultFile.createNewFile(); FileOutputStream
		 * fos = new FileOutputStream(resultFile); XMLEncoder encoder = new
		 * XMLEncoder(new BufferedOutputStream(fos));
		 * encoder.writeObject(result); encoder.close(); }catch (Exception e) {
		 * // TODO: handle exception e.printStackTrace(); }
		 */

	}

}
